/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.game;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.game.NumberGameDao;
import com.boxin.ims.modules.wechat.entity.LotteryConfig;
import com.boxin.ims.modules.wechat.entity.game.NumberGame;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 数字抽奖游戏Service
 * @author Jakemanse
 * @version 2014-08-22
 */
@Component
@Transactional(readOnly = true)
public class NumberGameService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(NumberGameService.class);
	
	@Autowired
	private NumberGameDao numberGameDao;
	
	public NumberGame get(Long id) {
		return numberGameDao.findOne(id);
	}
	
	
	public NumberGame getByUserId(Long userId) {
		DetachedCriteria dc = numberGameDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		List<NumberGame> results = numberGameDao.find(dc);
		return (results==null || results.isEmpty()) ? null : results.get(0);
	}
	
	
	public Page<NumberGame> find(Page<NumberGame> page, NumberGame numberGame) {
		DetachedCriteria dc = numberGameDao.createDetachedCriteria();
		if (numberGame.getUser()!=null && numberGame.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", numberGame.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(numberGame.getName())){
			dc.add(Restrictions.like("name", "%"+numberGame.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return numberGameDao.find(page, dc);
	}
	
	
	
	public List<NumberGame> find(NumberGame numberGame) {
		DetachedCriteria dc = numberGameDao.createDetachedCriteria();
		if (numberGame.getUser()!=null && numberGame.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", numberGame.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(numberGame.getName())){
			dc.add(Restrictions.like("name", "%"+numberGame.getName()+"%"));
		}
		
		
		if (StringUtils.isNotEmpty(numberGame.getStatus())){
			dc.add(Restrictions.eq("status", numberGame.getStatus()));
		}
		
		dc.addOrder(Order.desc("id"));
		return numberGameDao.find(dc);
	}
	
	
	
	
	
	@Transactional(readOnly = false)
	public void save(NumberGame numberGame) {
		numberGameDao.save(numberGame);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		numberGameDao.deleteById(id);
	}
	
}
